package com.ehotting.edsta.csharpServer.service.impl;

import java.util.List;

import com.ehotting.eaf.core.utils.DateUtils;
import com.ehotting.eaf.security.service.TokenService;
import com.ehotting.edsta.csharpServer.domain.ProjectInfo;
import com.ehotting.edsta.csharpServer.domain.vo.ProjectInfoRes;
import com.ehotting.edsta.csharpServer.domain.vo.ProjectInfoVo;
import com.ehotting.edsta.csharpServer.mapper.ProjectInfoMapper;
import com.ehotting.edsta.csharpServer.mapper.ProjectSimulationMapper;
import com.ehotting.edsta.csharpServer.service.IProjectInfoService;
import com.ehotting.eaf.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 工程信息Service业务层处理
 * 
 * @author edsta
 * @date 2023-04-11
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {

    @Autowired
    private TokenService tokenService;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private ProjectSimulationMapper projectSimulationMapper;

    /**
     * 查询工程信息
     * 
     * @param id 工程信息主键
     * @return 工程信息
     */
    @Override
    public ProjectInfo selectProjectInfoById(Long id)
    {
        return projectInfoMapper.selectProjectInfoById(id);
    }

    /**
     * 根据用户查询工程列表
     */
    @Override
    public List<ProjectInfo> selectProjectInfoListByUser() {
        LoginUser loginUser = tokenService.getLoginUser();
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setUserId(loginUser.getUserid());
        return selectProjectInfoList(projectInfo);
    }

    /**
     * 查询工程信息列表
     * 
     * @param projectInfo 工程信息
     * @return 工程信息
     */
    @Override
    public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo) {
        return projectInfoMapper.selectProjectInfoList(projectInfo);
    }

    /**
     * 查询工程信息列表
     *
     * @param projectInfo 工程信息
     * @return 工程信息
     */
    @Override
    public List<ProjectInfoRes> getProjectInfoList(ProjectInfo projectInfo) {
        return projectInfoMapper.getProjectInfoList(projectInfo);
    }

    /**
     * 新增工程信息
     * 
     * @param projectInfoVo 工程信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProjectInfo insertProjectInfo(ProjectInfoVo projectInfoVo) {
        LoginUser loginUser = tokenService.getLoginUser();
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setCreateTime(DateUtils.getNowDate());
        projectInfo.setName(projectInfoVo.getName());
        projectInfo.setPcbFile(projectInfoVo.getPcbFile());
        projectInfo.setUserId(loginUser.getUserid());
        projectInfoMapper.insertProjectInfo(projectInfo);
        return projectInfo;
    }

    /**
     * 修改工程信息
     * 
     * @param projectInfo 工程信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateProjectInfo(ProjectInfo projectInfo) {
        return projectInfoMapper.updateProjectInfo(projectInfo);
    }

    /**
     * 批量删除工程信息
     * 
     * @param ids 需要删除的工程信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProjectInfoByIds(Long[] ids) {
        projectSimulationMapper.deleteProjectSimulationByProjectIds(ids);
        return projectInfoMapper.deleteProjectInfoByIds(ids);
    }

    /**
     * 删除工程信息信息
     * 
     * @param id 工程信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProjectInfoById(Long id) {
        projectSimulationMapper.deleteProjectSimulationByProjectId(id);
        return projectInfoMapper.deleteProjectInfoById(id);
    }
}
